package com.adingxiong.quickstart.project.controller.util;

import com.adingxiong.quickstart.result.Result;
import com.adingxiong.quickstart.result.RetCode;
import com.adingxiong.quickstart.util.FastDFSClient;
import com.github.tobato.fastdfs.domain.fdfs.FileInfo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

/**
 * @ClassName UploadController
 * @Description TODO
 * @Author xiongchao
 * @Date 2020/5/27 15:53
 **/
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理")
public class UploadController {

    @Autowired
    private FastDFSClient fastDFSClient;


    @ApiOperation(value = "文件上传")
    @RequestMapping(value = "/upload" ,method = RequestMethod.POST)
    public Result upload(
            @RequestParam("file")  MultipartFile file
    ){

        if(file == null || file.isEmpty()){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),null);
        }
        Map<String,String> path= fastDFSClient.upload(file);
        return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),path);

    }

    @ApiOperation(value = "删除文件")
    @RequestMapping(value = "/del" ,method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "文件全路径" ,name = "url" ,required = true,dataType = "String",paramType = "query")
    })
    public Result delFile(String url){
        if(url == null || url.equals("")){
            return new Result(RetCode.PARAM_ERROR.getCode(),RetCode.PARAM_ERROR.getMsg(),null);
        }
        try {
            fastDFSClient.delFile(url);
            return new Result(RetCode.SUCCESS.getCode(),RetCode.SUCCESS.getMsg(),null);
        } catch (Exception e){
            return new Result(RetCode.PARAM_VALUE_ERROR.getCode(),RetCode.PARAM_VALUE_ERROR.getMsg(),null);
        }


    }

    @ApiOperation(value = "文件下载")
    @GetMapping("/downLoad")
    public ResponseEntity<byte []> downLoadFile(
            @ApiParam(value = "文件路径",required = true) @RequestParam(value = "url" ,required = true) String url
    ) {
        try {
            byte[] bytes = fastDFSClient.downloadFile(url);

            String fileName =url.substring(url.lastIndexOf("/" +1));
            fileName = new String(fileName.getBytes("utf-8"),"ISO_8859_1");

            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            httpHeaders.setContentDispositionFormData("attachment", fileName);

            return new ResponseEntity<byte[]>(bytes,httpHeaders,HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
